package com.zq.vaccinum.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zq.vaccinum.domain.entity.Vaccinum;
import com.zq.vaccinum.domain.entity.VaccinumType;
import com.zq.vaccinum.service.IVaccinumService;
import com.zq.vaccinum.service.IVaccinumTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 疫苗 前端控制器
 * </p>
 *
 * @author zq
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/vaccinum")
public class VaccinumController {
    //业务层
    @Autowired
    IVaccinumService vaccinumService;
    @Autowired
    IVaccinumTypeService typeService;
    //json解析工具
    ObjectMapper objectMapper = new ObjectMapper();

    //疫苗新增
    @RequestMapping("/insert")
    public String insert(Vaccinum vaccinum) throws JsonProcessingException {
        //map
        HashMap result = new HashMap();
        //使用业务层方法来执行添加操作
        boolean save = vaccinumService.save(vaccinum);
        //把结果添加到map中
        result.put("ok", save);
        //返回
        return objectMapper.writeValueAsString(result);
    }




    @RequestMapping("/queryAll")
    public String queryAll() throws JsonProcessingException {
        HashMap hashMap = new HashMap();
        List<Vaccinum> list = vaccinumService.list();
        hashMap.put("list",list);
        return objectMapper.writeValueAsString(hashMap);
    }


    @RequestMapping("/queryPage")
    public String listPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(required = false, defaultValue = "") String keyword ) throws JsonProcessingException {
        //        map
        HashMap result = new HashMap();
//        查询list   分页查询使用page方法入参分页信息                          // 查询的页码  每页显示的条数
        Page<Vaccinum> page = vaccinumService.page(
                new Page<Vaccinum>(pageNum, pageSize),
                new QueryWrapper<Vaccinum>().like(keyword != null && !keyword.equals(""), "name", keyword)
        );
//        通过page来获取集合
        List<Vaccinum> list = page.getRecords();
//       遍历集合，给每个对象赋值分类名称
        list.forEach(entity -> {
            //根据分类id查询分类名
            VaccinumType type = typeService.getById(entity.getType());
            //把分类名赋值到对象中即可
            entity.setTypeName(type.getName());
        });
        result.put("list", list);
        result.put("total", page.getTotal());
//        解析、返回json
        return objectMapper.writeValueAsString(result);
    }


    @RequestMapping("/list")
    public String list(
            @RequestParam(required = false, defaultValue = "") String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "4") Integer pageSize
    ) throws JsonProcessingException {
//        map
        HashMap result = new HashMap();
//        查询list   分页查询使用page方法入参分页信息                          // 查询的页码  每页显示的条数
        Page<Vaccinum> page = vaccinumService.page(
                new Page<Vaccinum>(pageNum, pageSize),
                new QueryWrapper<Vaccinum>().like(keyword != null && !keyword.equals(""), "name", keyword)
        );
//        通过page来获取集合
        List<Vaccinum> list = page.getRecords();
//       遍历集合，给每个对象赋值分类名称
        list.forEach(entity -> {
            //根据分类id查询分类名
            VaccinumType type = typeService.getById(entity.getType());
            //把分类名赋值到对象中即可
            entity.setTypeName(type.getName());

        });
        result.put("list", list);
        result.put("total", page.getTotal());
//        解析、返回json
        return objectMapper.writeValueAsString(result);
    }
    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(int id) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean b = vaccinumService.removeById(id);
        result.put("ok", b);
        return objectMapper.writeValueAsString(result);
    }

    @RequestMapping("/update")
    public String update(Vaccinum vaccinum) throws JsonProcessingException {
        HashMap result = new HashMap();
        boolean update = vaccinumService.updateById(vaccinum);
        result.put("ok", update);
        return objectMapper.writeValueAsString(result);
    }


}
